from socket import socket,AF_INET,SOCK_STREAM,gethostbyname,gethostname
from time import sleep
from select import select

def send_data(data):
    #  创建客户端
    soc = socket(AF_INET,SOCK_STREAM)
    #  连接
    soc.connect(('localhost', 6666))
    #soc.connect(('h7153156i4.zicp.fun',36117))#tcp://h7153156i4.zicp.fun:36117 '192.168.199.213', 6666
    #soc.connect(('115.236.153.177',443))
    #data =#input("请输入要发送客户端的测试数据:")
    soc.send(data.encode())
    sleep(5)
    #info=soc.recv(1024).decode('utf-8')
    #print(info)
    soc.close()
    #return info
    # for data in [b'mary', b'bob', b'jack']:
    #     #   发送数据
    #     soc.send(data)
    #     print(soc.recv(1024).decode('utf-8'))
    # soc.send(b'exit')
    # #  套字节关闭
    # soc.close()

def receive_command(max_time=15):
    #  创建客户端
    soc = socket(AF_INET,SOCK_STREAM)
    soc.connect(('h7153156i4.zicp.fun', 36117))
    #local_ip = gethostbyname(gethostname())
    #  连接
    #soc.connect(('192.168.199.213', 6666))#local_ip
    if max_time!=None:
        soc.settimeout(max_time)
    print('创建客户端')
    try:
        command = soc.recv(1024).decode('utf-8')
        soc.close()
        if command == '':
            print('无指令')
            return 'timeout'
        else:
            return command
    except Exception as e:
        print(e)
        soc.close()
        return 'no command'

    # soc.setblocking(0)
    # ready =select([soc], [], [], 10)#5
    # if ready[0]:
    #     command = soc.recv(1024).decode('utf-8')
    #     soc.close()
    #     if command=='':
    #         return 'timeout'
    #     else:
    #         return command
    # else:
    #     return 'no command'


if __name__ =="__main__":
    # count=str(1)
    # info=send_data(count)#'1cm2' f'{count}'
    # #send_data('12')
    # print('info:',info)

   print(receive_command())